package com.lbl.listener;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import java.util.Timer;
import java.util.TimerTask;

@WebListener()
public class Demo1Listener implements ServletContextListener{

    public Demo1Listener() {
    }

    //这个方法用来监听ServletContext域的创建
     /*
        这个域：服务器启动时创建
     */
    public void contextInitialized(ServletContextEvent sce) {
        //设置一个定时器
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                try {
                    System.out.println("--定时发送邮件");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }, 5000, 2000);
        System.out.println("ServletContext域的创建啦");
    }

    //这个方法用来监听ServletContext域的销毁
    /*
       这个域：服务器关闭时销毁
     */
    public void contextDestroyed(ServletContextEvent sce) {
        System.out.println("ServletContext域销毁啦");
    }
}
